package com.yunqian.web;

import com.alibaba.fastjson.JSONObject;
import com.yunqian.redot.constant.OrderStatus;
import com.yunqian.redot.domain.TicketOrder;
import com.yunqian.redot.service.TicketOrderService;
import com.yunqian.util.ZPTUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author czy
 * @describe
 * @date 2018/10/27
 */
@Log4j2
@RestController
@RequestMapping("/api/zpt")
public class ZPTController {

    @Autowired
    private TicketOrderService ticketOrderService;

    /**
     * 核销通知
     */
    @RequestMapping("/notify")
    public Map inform(HttpServletRequest request) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer resultId = 1;
        String resultMsg = "接口请求成功";
        String jsonStr = ZPTUtil.callBack(request);
        try {
            JSONObject obj = JSONObject.parseObject(jsonStr);
            String srcOrderID = obj.getJSONObject("body").getString("srcOrderID");
            TicketOrder order = ticketOrderService.findByOrderNo(srcOrderID);
            order.setStatus(OrderStatus.HAVE_THE_TICKETS.getCode());
            ticketOrderService.update(order);
        } catch (Exception e) {
            resultId = 3;
            resultMsg = "请求失败，json格式错误";
        }
        Map<String, Map<String, Object>> map = new HashMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("resultId", resultId);
        head.put("resultMsg", resultMsg);
        head.put("resultTime", dateFormat.format(new Date()));
        map.put("head", head);
        // 响应数据
        return map;

    }

}
